<template>
  <div class="app-container">
    <el-form ref="form" :rules="rules" :model="form" label-width="120px">
      <h4 style="border-left: mediumseagreen solid; padding-left: 10px">团购介绍</h4>
      <el-form-item label="团购标题" prop="title">
        <el-input v-model="form.title"/>
      </el-form-item>
      <el-form-item label="团购内容">
        <div class="group-content">
          <el-upload
            ref="upload"
            :auto-upload="false"
            action="https://jsonplaceholder.typicode.com/posts/"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
          <el-input
            v-model="form.contentText"
            type="textarea"
            rows="3"
            placeholder="请输入团购活动内容"
            maxlength="300"
            show-word-limit
            style="border: none; margin-top: 5px"
          />
        </div>
      </el-form-item>
      <h4 style="border-left: mediumseagreen solid; padding-left: 10px">团购商品</h4>
      <el-form-item label="团购商品" prop="spuId">
        <div v-if="form.spuId===''">
          <el-button type="info" plain>从商品库导入</el-button>
          <el-button type="info" plain>创建商品</el-button>
        </div>
        <el-table
          v-else
          :data="form.product"
          border
          style="width: 100%"
        >
          <el-table-column
            type="index"
            label="序号"
            width="60"
            align="true"
          />
          <el-table-column
            prop="name"
            label="商品名称"
            width="130"
            header-align="true"
            show-overflow-tooltip="true"
          />
          <el-table-column
            prop="attribute"
            label="规格"
            header-align="true"
            show-overflow-tooltip="true"
          />
          <el-table-column
            prop="category"
            label="分类"
            header-align="true"
            show-overflow-tooltip="true"
          />
          <el-table-column
            prop="stock"
            label="库存"
            width="100"
            header-align="true"
            show-overflow-tooltip="true"
          />
          <el-table-column
            prop="limit"
            label="是否限购"
            width="100"
            header-align="true"
            show-overflow-tooltip="true"
          />
          <el-table-column
            label="操作"
            width="50"
            align="true"
          >
            <template slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                circle
                @click="handleProductEdit(scope.row)"
              />
              <el-button type="warning" icon="el-icon-star-off" circle/>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <h4 style="border-left: mediumseagreen solid; padding-left: 10px">团购设置</h4>
      <el-form-item label="团购时间" prop="datetime">
        <el-date-picker
          v-model="form.datetime"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">创建</el-button>
        <el-button @click="resetForm('form')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        title: '',
        contentText: '',
        spuId: '',
        datetime: ''
      },
      spu: [],
      attribute: [],
      sku: [],
      dialogImageUrl: '',
      dialogVisible: false,
      pickerOptions: {
        shortcuts: [{
          text: '最近7天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            end.setTime(start.getTime() + 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近30天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            end.setTime(start.getTime() + 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      rules: {
        title: [{ required: true, message: '请输入团购活动标题', trigger: 'blur' }],
        spuId: [{ required: true, message: '请选择团购商品', trigger: 'blur' }],
        datetime: [{ type: 'date', required: true, message: '请选择日期', trigger: 'change' }]
      }
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (new Date(this.form.datetime[1]) < new Date()) {
            this.$message.warning('团购结束时间需要大于当前时间!')
            return false
          }
          this.$refs.upload.submit()
          this.$message('submit!')
        } else return false
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleProductEdit(product) {
      console.log(product)
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}

.group-content {
  border: rgba(152, 154, 156, 0.27) solid 1px;
  border-radius: 5px;
  padding: 10px;
}
</style>

